import Settings from '../config/Settings.js';

export default class Boss {
  constructor(game) {
    this.game = game;
    
    this.x = game.canvas.width + Settings.boss.width;
    this.y = game.canvas.height - 200;
    this.width = Settings.boss.width;
    this.height = Settings.boss.height;
    
    this.health = Settings.boss.health;
    this.pattern = 'enter'; // 初始状态为进场
    this.attackTimer = 0;
    this.invincibleTimer = 0;
    
    // 添加移动相关属性
    this.velocity = {
      x: 0,
      y: 0
    };
    this.moveDirection = -1; // -1 左移，1 右移
    this.moveTimer = 0;
    this.jumpCooldown = 0;
    this.isGrounded = false;
    
    // 状态标记
    this.isCharging = false;
    this.chargeTarget = null;
    this.stunned = false;
    this.stunTimer = 0;
    
    this.projectiles = [];
  }
  
  update() {
    // 更新无敌时间
    if (this.invincibleTimer > 0) {
      this.invincibleTimer--;
    }
    
    // 更新眩晕状态
    if (this.stunned) {
      this.stunTimer--;
      if (this.stunTimer <= 0) {
        this.stunned = false;
        this.pattern = 'move'; // 恢复正常移动
      }
      return;
    }
    
    // 更新攻击计时器
    this.attackTimer++;
    
    // 根据不同模式更新行为
    switch(this.pattern) {
      case 'enter':
        // 从右侧进场
        this.x -= 3;
        if (this.x <= this.game.canvas.width - 200) {
          this.pattern = 'move';
        }
        break;
        
      case 'move':
        // 左右移动并寻找机会跳跃
        this.moveTimer++;
        
        // 基础移动
        this.x += this.moveDirection * 3;
        
        // 到达边界时改变方向
        if (this.x <= 100) {
          this.moveDirection = 1;
        } else if (this.x >= this.game.canvas.width - 200) {
          this.moveDirection = -1;
        }
        
        // 随机跳跃
        if (this.isGrounded && this.jumpCooldown <= 0 && Math.random() < 0.02) {
          this.velocity.y = -15;
          this.jumpCooldown = 60;
        }
        
        // 检测玩家位置，决定是否发起冲锋
        const player = this.game.player;
        const distToPlayer = Math.abs(player.x - this.x);
        
        if (this.attackTimer >= Settings.boss.attackInterval && distToPlayer < 300) {
          this.pattern = 'charge';
          this.chargeTarget = player.x;
          this.isCharging = true;
          this.attackTimer = 0;
        }
        break;
        
      case 'charge':
        // 冲锋攻击
        if (this.isCharging) {
          const chargeSpeed = 8;
          const direction = this.chargeTarget > this.x ? 1 : -1;
          this.x += direction * chargeSpeed;
          
          // 冲锋结束条件
          if (Math.abs(this.x - this.chargeTarget) < 50) {
            this.isCharging = false;
            this.pattern = 'move';
          }
        }
        break;
    }
    
    // 应用重力
    if (!this.isGrounded) {
      this.velocity.y += 0.5;
    }
    this.y += this.velocity.y;
    
    // 地面碰撞检测
    if (this.y + this.height >= this.game.canvas.height - 60) {
      this.y = this.game.canvas.height - 60 - this.height;
      this.velocity.y = 0;
      this.isGrounded = true;
    } else {
      this.isGrounded = false;
    }
    
    // 更新跳跃冷却
    if (this.jumpCooldown > 0) {
      this.jumpCooldown--;
    }
    
    // 更新投射物
    this.updateProjectiles();
  }
  
  takeDamage(amount) {
    if (this.invincibleTimer > 0) return false;
    
    this.health -= amount || Settings.boss.damageFromJump;
    this.invincibleTimer = Settings.boss.invincibleTime;
    
    // 受到伤害后短暂眩晕
    this.stunned = true;
    this.stunTimer = 30;
    this.velocity.y = -5; // 轻微击退效果
    
    // 播放受伤特效
    this.game.particleSystem.createParticles(
      this.x + this.width/2,
      this.y + this.height/2,
      '#FF0000',
      15,
      5,
      5,
      30
    );
    
    // 检查是否被击败
    return this.health <= 0;
  }
  
  render(ctx) {
    ctx.save();
    
    // 受伤闪烁效果
    if (this.invincibleTimer > 0 && this.invincibleTimer % 6 < 3) {
      ctx.globalAlpha = 0.5;
    }
    
    // 绘制 Boss
    ctx.fillStyle = this.stunned ? '#A0A0A0' : '#FF0000';
    ctx.fillRect(this.x, this.y, this.width, this.height);
    
    // 绘制血条
    const healthBarWidth = this.width;
    const healthBarHeight = 10;
    const healthPercentage = this.health / Settings.boss.health;
    
    ctx.fillStyle = '#000000';
    ctx.fillRect(this.x, this.y - 20, healthBarWidth, healthBarHeight);
    ctx.fillStyle = '#FF0000';
    ctx.fillRect(this.x, this.y - 20, healthBarWidth * healthPercentage, healthBarHeight);
    
    ctx.restore();
    
    // 绘制投射物
    ctx.fillStyle = '#FFA500';
    for (const projectile of this.projectiles) {
      ctx.fillRect(projectile.x, projectile.y, 20, 20);
    }
  }
  
  updateProjectiles() {
    for (let i = this.projectiles.length - 1; i >= 0; i--) {
      const projectile = this.projectiles[i];
      
      // 移动投射物
      projectile.x -= projectile.speed;
      
      // 检查是否离开屏幕
      if (projectile.x < -20) {
        this.projectiles.splice(i, 1);
        continue;
      }
      
      // 检查与玩家的碰撞
      if (this.checkProjectileCollision(projectile, this.game.player)) {
        this.projectiles.splice(i, 1);
        this.game.player.takeDamage();
      }
    }
  }
  
  checkProjectileCollision(projectile, player) {
    return (
      projectile.x < player.x + player.width / 2 &&
      projectile.x + 20 > player.x - player.width / 2 &&
      projectile.y < player.y + player.height &&
      projectile.y + 20 > player.y
    );
  }
} 